package com.burgeon.backend.service.Impl;

import com.burgeon.backend.Rest.Tool.*;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.service.FromToolService;
import com.burgeon.backend.service.ex.GetUserDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FromToolServiceImpl implements FromToolService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public List GetCustomer(String account) {
        Info info = GetInfoByAccount(account);
        GetCustomer getCustomer = new GetCustomer(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            return getCustomer.getData();
        } catch (Exception e) {
            return null;
        }
    }

    public List GetRegion(String token)throws GetUserDataException{
        Info info = GetInfo(token);
        GetRegion getRegion = new GetRegion(info.getUrl(),info.getAccount(),info.getPassword());
        List data;
        try {
           data = getRegion.getData();
        } catch (Exception e) {

            throw new GetUserDataException();
        }

        return data;
    }

    @Override
    public List GetStore(String name, String account) throws GetUserDataException {
        Info info = GetInfoByAccount(account);
        GetStore getStore = new GetStore(info.getUrl(),info.getAccount(),info.getPassword());
        List data;
        try {
            data = getStore.getData(name);
        } catch (Exception e) {

            throw new GetUserDataException();
        }

        return data;
    }

    @Override
    public List GetEMP(String name, String account) throws GetUserDataException {
        Info info = GetInfoByAccount(account);
        GetEMP getEMP = new GetEMP(info.getUrl(),info.getAccount(),info.getPassword());
        List data;
        try {
            data = getEMP.getData(name);
        } catch (Exception e) {

            throw new GetUserDataException();
        }
        return data;
    }

    @Override
    public String GetMVIPTYPE(String token) throws GetUserDataException {
        Info info = GetInfo(token);
        GetVIPTypeDefault getVIPTypeDefault = new GetVIPTypeDefault(info.getUrl(),info.getAccount(),info.getPassword());
        String data;
        try {
            data = getVIPTypeDefault.getData();
        } catch (Exception e) {

            throw new GetUserDataException();
        }
        return data;
    }

    private Info GetInfo(String token){
        return accountMapper.GetTokenData(token);
    }
    private Info GetInfoByAccount(String account){
        return accountMapper.GetAccData(account);
    }
}

